package com.wulian.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DateUtil {

	public final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
	public final static SimpleDateFormat sdf2 = new SimpleDateFormat("YYYYMMdd");
	public final static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	public final static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd HH");
	private static DateFormat dateFormat = null;// 时间格式化
	public final static SimpleDateFormat sdf5 = new SimpleDateFormat("MM月dd日");
	public final static SimpleDateFormat sdf6 = new SimpleDateFormat("yyyy年MM月dd日");

	/**
	 * 获取当前时间
	 * @return
	 */
	public static Date getSysTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.getTime();
    }
	
	public static Date parseDate(String date) {
		Date date1 = null;
		try {
			date1 = sdf.parse(date);
		} catch (ParseException e) {
			log.error("日期解析错误", e);
		}
		return date1;
	}
	public static Date parseDate2(String date) {
		Date date1 = null;
		try {
			date1 = sdf1.parse(date);
		} catch (ParseException e) {
			log.error("日期解析错误", e);
		}
		return date1;
	}
	
	public static Date parseDate3(String date) {
		Date date1 = null;
		try {
			date1 = sdf2.parse(date);
		} catch (ParseException e) {
			log.error("日期解析错误", e);
		}
		return date1;
	}
	
	public static Date parseDate4(String date) {
		Date date1 = null;
		try {
			date1 = sdf3.parse(date);
		} catch (ParseException e) {
			log.error("日期解析错误", e);
		}
		return date1;
	}



	public static String DateFormat(Date date) {
		String format = sdf.format(date);
		return format;
	}

	public static String DateFormat2(Date date) {
		String format = sdf1.format(date);
		return format;
	}
	
	public static String DateFormat3(Date date) {
		String format = sdf2.format(date);
		return format;
	}
	public static String DateFormat4(Date date) {
		String format = sdf3.format(date);
		return format;
	}
	public static String DateFormat5(Date date) {
		String format = sdf4.format(date);
		return format;
	}
	
	public static String DateFormat6(Date date) {
		String format = sdf6.format(date);
		return format;
	}

	/**
	 * 指定日期几天后或者几天前的日期
	 * 
	 * @author jin.yp
	 * @date 2018-08-16
	 * @param paramDate
	 *            指定日期
	 * @param days
	 *            天数
	 * @return Date 几天后或者几天前的日期
	 */
	public static Date addDate(Date paramDate, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(paramDate);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}
	
	public static Date addYear(Date paramDate, int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(paramDate);
		calendar.add(Calendar.YEAR, year);
		return calendar.getTime();
	}
	
	/**
     * 通过制定的格式，将Date对象格式化为字符串
     * 
     * @param date 需要转换的Date对象
     * @param formatStr 转换的格式
     * @return 转换之后的字符串
     */
    public static String dateToStr(Date date) {
        String result = null;
        if (date != null) {
            result = sdf1.format(date);
        }
        return result;
    }

	/**
	 * 获取昨天日期(-1)
	 *
	 * @return
	 */
	public static Date getYesterday(int i) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, i);
		return cal.getTime();
	}
    
    
    /**
     * 
        * @Title: dateToStr
        * @Description: TODO(这里用一句话描述这个方法的作用)
        * @param @param date type // 0 -- 本天    1 --  本周     2  --- 本月       3 --  近30 天   4 -- 本年
        * @param @return    参数  
        * @return String    返回类型
    	* @author 李东旭  
    	* @date 2019年11月12日  
        * @throws
     */
    public static Map<String , Date> getDate(String type) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    	Map<String , Date> ret = new HashMap<String, Date>();
    	if("0".equals(type)) {
    		Date now = new Date();
    	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//可以方便地修改日期格式
    	    String MTime = dateFormat.format( now );
    	    String startDate = MTime + " 00:00:00";
    	    String endDate = MTime + " 23:59:59";
    	    Date startDateTime = new Date();
    	    Date endDateTime = new Date();
    	    try {
				startDateTime = sdf.parse(startDate);
				endDateTime =  sdf.parse(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	    ret.put("start", startDateTime);
    	    ret.put("end", endDateTime);
    	}else if ("1".equals(type)) {
    		Date now = new Date();
    	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//可以方便地修改日期格式
    	    String MTime = dateFormat.format( now );
    	    Date date1=null;
    	    try {
    	        date1 = dateFormat.parse(MTime);//系统当前时间
    	    } catch (ParseException e) {
    	        e.printStackTrace();
    	    }
    	    Calendar cal = Calendar.getInstance();
    	    cal.setTime(date1);

    	    cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前一天.

    	    cal.add(Calendar.DAY_OF_MONTH, +1);//取当前日期的后一天.
    	    // hour中存的就是星期几了，其范围 1~7
    	    // 1=星期日 7=星期六，其他类推
    	    int hour=cal.get(Calendar.DAY_OF_WEEK);
    	    String stime=null;
    	    String etime=null;
    	    switch (hour) {
    	        case 2:
    	            //周一
    	            stime=MTime;
    	            cal.add(Calendar.DAY_OF_MONTH, +6);//获取当前日期的后六天.
    	            String  date2=dateFormat.format(cal.getTime());
    	            etime=date2;
    	            break;
    	        case 3:
    	            //周二
    	            cal.add(Calendar.DAY_OF_MONTH, -1);//获取当前日期的前一天.
    	            String  date12=dateFormat.format(cal.getTime());
    	            stime=date12;
    	            cal.add(Calendar.DAY_OF_MONTH, +6);//获取周日时间
    	            String  date22=dateFormat.format(cal.getTime());
    	            etime=date22;
    	            break;
    	        case 4:
    	            //周三
    	            cal.add(Calendar.DAY_OF_MONTH, -2);
    	            String  date13=dateFormat.format(cal.getTime());//取当前日期的前两天.
    	            stime=date13;
    	            cal.add(Calendar.DAY_OF_MONTH, +6);//获取周日时间
    	            String  date23=dateFormat.format(cal.getTime());
    	            etime=date23;
    	            break;
    	        case 5:
    	            //周四
    	            cal.add(Calendar.DAY_OF_MONTH, -3);//获取取当前日期的前三天.
    	            String  date14=dateFormat.format(cal.getTime());
    	            stime=date14;
    	            cal.add(Calendar.DAY_OF_MONTH, +6);//获取周日时间
    	            String  date24=dateFormat.format(cal.getTime());
    	            etime=date24;
    	            break;
    	        case 6:
    	            //周五
    	            cal.add(Calendar.DAY_OF_MONTH, -4);//获取取当前日期的前四天.
    	            String  date15=dateFormat.format(cal.getTime());
    	            stime=date15;
    	            cal.add(Calendar.DAY_OF_MONTH, +6);//获取周日时间
    	            String  date25=dateFormat.format(cal.getTime());
    	            etime=date25;
    	            break;
    	        case 7:
    	            //周六
    	            cal.add(Calendar.DAY_OF_MONTH, -5);//获取取当前日期的前五天.
    	            String  date16=dateFormat.format(cal.getTime());
    	            stime=date16;
    	            cal.add(Calendar.DAY_OF_MONTH, +6);//获取周日时间
    	            String  date26=dateFormat.format(cal.getTime());
    	            etime=date26;
    	            break;
    	        default:
    	            //周日
    	            cal.add(Calendar.DAY_OF_MONTH, -6);
    	            String  date7=dateFormat.format(cal.getTime());//取当前日期的前六天.
    	            stime=date7;
    	            etime=MTime;
    	            break;
    	    }
    	    String startDate = stime + " 00:00:00";
    	    String endDate = etime + " 23:59:59";
    	    Date startDateTime = new Date();
    	    Date endDateTime = new Date();
    	    try {
				startDateTime = sdf.parse(startDate);
				endDateTime =  sdf.parse(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	    ret.put("start", startDateTime);
    	    ret.put("end", endDateTime);
    	}else if ("2".equals(type)) {
    		
    		Date now = new Date();
    	    Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            // 设置为1号,当前日期既为本月第一天
            cal.set(Calendar.DAY_OF_MONTH, 1);
            Date startDate = cal.getTime();
            String startDateVal = sdf1.format(startDate) + " 00:00:00";
    	    String endDate = sdf1.format(now) + " 23:59:59";
    	    
    	    Date startDateTime = new Date();
    	    Date endDateTime = new Date();
    	    try {
				startDateTime = sdf.parse(startDateVal);
				endDateTime =  sdf.parse(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	    ret.put("start", startDateTime);
    	    ret.put("end", endDateTime);
            
    	}else if ("3".equals(type)) {
    		Date today = new Date();
    		String endDate = sdf1.format(today)+ " 23:59:59";//当前日期
    		//获取三十天前日期
    		Calendar theCa = Calendar.getInstance();
    		theCa.setTime(today);
    		theCa.add(theCa.DATE, -29);//最后一个数字30可改，30天的意思
    		Date start = theCa.getTime();
    		String startDate = sdf1.format(start)+ " 00:00:00";//三十天之前日期
    	    Date startDateTime = new Date();
    	    Date endDateTime = new Date();
    	    try {
				startDateTime = sdf.parse(startDate);
				endDateTime =  sdf.parse(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	    ret.put("start", startDateTime);
    	    ret.put("end", endDateTime);
            
    	}else if("4".equals(type)) {
    		Date today = new Date();
    		String endDate = sdf1.format(today)+ " 23:59:59";//当前日期
    	    Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
    		//--获取当前年份
    		int year = aCalendar.get(Calendar.YEAR);
    		String startDate = year + "-01-01"+ " 00:00:00";//三十天之前日期
    	    Date startDateTime = new Date();
    	    Date endDateTime = new Date();
    	    try {
				startDateTime = sdf.parse(startDate);
				endDateTime =  sdf.parse(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	    ret.put("start", startDateTime);
    	    ret.put("end", endDateTime);
            
    	}
        return ret;
    }

    public static int getDiffYear(Date startDate1, Date endDate){
		Calendar bef = Calendar.getInstance();
		Calendar aft = Calendar.getInstance();
		bef.setTime(startDate1);
		aft.setTime(endDate);
		int surplus = aft.get(Calendar.DATE) - bef.get(Calendar.DATE);
		int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
		int year = aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR);
		if(result < 0){
			result = 1;
		}else if(result == 0){
			result = surplus <= 0 ? 0 : 1;
		}else{
			result = 0;
		}
		log.info("相差年份：" + (year + result));
		return year + result;
	}
    
    /**
     * 获取当前时间(毫秒)
     *
     * @return
     */
    public static long getCurTimeMillis() {
        return System.currentTimeMillis();
    }
	


	/**
	 * 功能描述: <br>
	 * 〈获取当天时间的开始时间〉
	 * @Param:
	 * @Return:
	 * @Author: caifulin
	 * @Date: 2020/5/22
	 */
	public static Date getToDateStart(){
    	Date date = new Date();
    	/*Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		Date time = calendar.getTime();
		return time;*/
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
		LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
		return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
	}
	/**
	 * 功能描述: <br>
	 * 〈获取当天时间的结束时间〉
	 * @Param:
	 * @Return:
	 * @Author: caifulin
	 * @Date: 2020/5/22
	 */
	public static Date getToDateEnd(){
		Date date = new Date();
		/*Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY,23);
		calendar.set(Calendar.MINUTE,59);
		calendar.set(Calendar.SECOND,59);
		Date time = calendar.getTime();
		return time;
		*/
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
		LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
		return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());


	}

	/**
	 * 功能描述: <br>
	 * 〈获取上周的今天-1天〉
	 * @Param:
	 * @Return:
	 * @Author: caifulin
	 * @Date: 2020/6/2
	 */
	public static Date getWeekDateStart(){
		Date date = new Date();
		String format = DateUtil.sdf1.format(date);
		Date parse = null;
		try{
			parse = DateUtil.sdf1.parse(format);
		}catch (Exception e){
			e.printStackTrace();
		}
		Calendar instance = Calendar.getInstance();
		instance.setTime(parse);
		instance.add(Calendar.DAY_OF_YEAR ,-6 );
		Date time = instance.getTime();
		return time;
	}

	/**
	 * 功能描述: <br>
	 * 〈近30天〉
	 * @Param:
	 * @Return:
	 * @Author: caifulin
	 * @Date: 2020/6/2
	 */
	public static Date getMonthDateStart(){
		Date date = new Date();
		String format = DateUtil.sdf1.format(date);
		Date parse = null;
		try{
			parse = DateUtil.sdf1.parse(format);
		}catch (Exception e){
			e.printStackTrace();
		}
		Calendar instance = Calendar.getInstance();
		instance.setTime(parse);
		instance.add(Calendar.DAY_OF_YEAR ,-29 );
		Date time = instance.getTime();
		return time;
	}
	
	 /* 
	 * @Title: dayForWeek
	 * @Description: (判断某一日期是星期几)
	 * @author 马志豪
	 * @param @param pTime
	 * @param @return
	 * @param @throws Throwable    设定文件
	 * @return String    返回类型
	 * @throws
	  */
	 public static String dayForWeek(String pTime) throws Throwable {  

	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  

	        Date tmpDate = format.parse(pTime);  

	        Calendar cal = Calendar.getInstance(); 

	        String[] weekDays = {"7","1", "2", "3", "4", "5", "6"};

	        try {

	            cal.setTime(tmpDate);

	        } catch (Exception e) {

	            e.printStackTrace();

	        }

	        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。

	        if (w < 0)

	            w = 0;

	        return weekDays[w];

	    }  


	public static void main(String[] args) throws Throwable{
		String dayForWeek = dayForWeek("2020-12-13");
		System.out.println(dayForWeek);
	}

}
